****FIGURE 6****

cd "$main_dir/output/figures/main"

graph set window fontface "Palatino"

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1 yob_ln_gdppc)
merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/winsz_inflation_var_exp.dta", nogen keepusing(sd_winsz_inflation)

local varlist prop_unrest no_exec_changes prop_affect_epidemic bank_crisis currency_crisis twin_crisis winsz_inflation
foreach var of local varlist {
	merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/`var'_var_exp.dta", nogen keepusing(total_`var'_obs w`var'_1)
}

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.

//--------------------------------------------------------
//Create Figure 6
//--------------------------------------------------------

matrix coef1 = J(8, 3, .)
	matrix coln coef1 = coefficients lcl ucl
	matrix rown coef1 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"

matrix coef2 = J(8, 3, .)
	matrix coln coef2 = coefficients lcl ucl
	matrix rown coef2 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"
	
matrix coef3 = J(8, 3, .)
	matrix coln coef3 = coefficients lcl ucl
	matrix rown coef3 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"

matrix coef4 = J(8, 3, .)
	matrix coln coef4 = coefficients lcl ucl
	matrix rown coef4 = "Unrest" "Executive Turnover" "Epidemic Exposure" "Banking Crises" "Currency Crises" "Financial Crises" "Inflation" "Inflation Volatility"

local i = 1
local varlist prop_unrest no_exec_changes prop_affect_epidemic bank_crisis currency_crisis twin_crisis winsz_inflation
foreach var of local varlist {
	
	qui reghdfe trust_govt wgrowth_rate_1 w`var'_1 yob_ln_gdppc if sample==1 & total_`var'_obs>=0.9*(age-1) & total_`var'_obs!=., absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode)
	
	cap drop z*
	local list trust_govt wgrowth_rate_1 yob_ln_gdppc w`var'_1
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_govt zwgrowth_rate_1 zw`var'_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode) level(90)
	
	qui mat os = r(table)
	qui mat coef1[`i',1] = os[1,1]
	qui mat coef1[`i',2] = os[5,1]
	qui mat coef1[`i',3] = os[6,1]
	
	qui mat coef2[`i',1] = os[1,2]
	qui mat coef2[`i',2] = os[5,2]
	qui mat coef2[`i',3] = os[6,2]
	
	qui mat coef3[`i',1] = os[1,1]
	qui mat coef3[`i',2] = os[1,1]-1.96*os[2,1]
	qui mat coef3[`i',3] = os[1,1]+1.96*os[2,1]
	
	qui mat coef4[`i',1] = os[1,2]
	qui mat coef4[`i',2] = os[1,2]-1.96*os[2,2]
	qui mat coef4[`i',3] = os[1,2]+1.96*os[2,2]
	
	di "`var' done"
	
	local ++i
}

qui reghdfe trust_govt wgrowth_rate_1 sd_winsz_inflation yob_ln_gdppc if sample==1 & total_winsz_inflation_obs>=0.9*(age-1) & total_winsz_inflation_obs!=., absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode) level(95)
	
cap drop z*
local list trust_govt wgrowth_rate_1 yob_ln_gdppc sd_winsz_inflation
foreach x of local list {
	egen z`x' = std(`x') if e(sample)==1
}

qui reghdfe ztrust_govt zwgrowth_rate_1 zsd_winsz_inflation zyob_ln_gdppc if e(sample)==1, absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode) level(90)

qui mat os = r(table)
qui mat coef1[8,1] = os[1,1]
qui mat coef1[8,2] = os[5,1]
qui mat coef1[8,3] = os[6,1]

qui mat coef2[8,1] = os[1,2]
qui mat coef2[8,2] = os[5,2]
qui mat coef2[8,3] = os[6,2]

qui mat coef3[8,1] = os[1,1]
qui mat coef3[8,2] = os[1,1]-1.96*os[2,1]
qui mat coef3[8,3] = os[1,1]+1.96*os[2,1]

qui mat coef4[8,1] = os[1,2]
qui mat coef4[8,2] = os[1,2]-1.96*os[2,2]
qui mat coef4[8,3] = os[1,2]+1.96*os[2,2]

coefplot (matrix(coef1[,1]), ci((coef1[,2] coef1[,3])) offset(-0.05) msymbol(diamond) mlcolor(navy) mfcolor(white) mlwidth(medthick) mcolor(navy) ciopts(color(navy) lwidth(*2)) label(Growth Experience)) ///
		 (matrix(coef3[,1]), ci((coef3[,2] coef3[,3])) offset(-0.05) msymbol(diamond) mlcolor(navy) mfcolor(white) mlwidth(medthick) mcolor(navy) ciopts(color(navy)) label(Growth Experience)) ///
         (matrix(coef2[,1]), ci((coef2[,2] coef2[,3])) offset(0.05) msymbol(square) mlcolor(maroon) mfcolor(white) mlwidth(medthick)  mcolor(maroon) ciopts(color(maroon) lwidth(*2)) label(Other Experience)) ///
	     (matrix(coef4[,1]), ci((coef4[,2] coef4[,3])) offset(0.05) msymbol(square) mlcolor(maroon) mfcolor(white) mlwidth(medthick)  mcolor(maroon) ciopts(color(maroon)) label(Other Experience)), ///
	     graphregion(color(white)) bgcolor(white) xline(0) legend(order(2 "Growth Experience" 6 "Other Experiences") size(small)) xscale(range(-0.1 0.15)) xlabel(-0.1(0.05)0.15, labs(small)) ylabel(,labs(small))
graph export "figure_6.png", replace	
graph export "figure_6.tif", replace as(tif) width(3600)

